//
//  ViewController.m
//  输入框跟随键盘移动
//
//  Created by zxy on 2018/4/24.
//  Copyright © 2018年 zssh. All rights reserved.
//

#import "ViewController.h"
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tfBottomCons;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}


-(void)keyboardFrameWillChange:(NSNotification *)no{
    //获取键盘弹起时间
    CGFloat duration = [no.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //获取键盘frame
    CGRect frame = [no.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //设置输入框约束
    self.tfBottomCons.constant = SCREEN_HEIGHT - frame.origin.y;
    
    [UIView animateWithDuration:duration animations:^{
       //刷新布局，使得工具条随键盘frame改变有动画
        [self.view layoutIfNeeded];
    }];
}


@end
